package com.neusoft.neuiotms.dcam.fixed.converter;

import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.GoodsFileDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Asset;
import com.neusoft.neuiotms.dcam.fixed.model.GoodsFile;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.GoodsFileRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.SpareTypeRepository;
import com.neusoft.neuiotms.dcam.fixed.vo.GoodsFileImportVO;

@Component
public class GoodsFileConverter extends AbstractConverter<GoodsFile, GoodsFileDTO> {

    @Autowired
    private GoodsFileRepository goodsFileRepository;

    @Autowired
    private SpareTypeRepository spareTypeRepository;
    @Autowired
    private AssetRepository assetRepository;

    @Override
    public GoodsFile toModel(final GoodsFileDTO dto) {
        final GoodsFile model;
        if (dto.isNew()) {
            model = new GoodsFile();
            model.setCode(UUID.randomUUID().toString());
        } else {
            model = this.goodsFileRepository.getOne(dto.getId());
            model.setCode(dto.getCode());
        }
        model.setName(dto.getName());
        model.setSpecification(dto.getSpecification());
        model.setDescription(dto.getDescription());
        model.setUpdateTime(new Date());
        model.setManagementOrgCode(dto.getManagementOrgCode());
        model.setManagementOrgName(dto.getManagementOrgName());

        if (dto.getSpareTypeCode() != null) {
            model.setSpareType(this.spareTypeRepository.findByCode(dto.getSpareTypeCode()));
        }
        model.setAssetCode(dto.getAssetCode());
        return model;
    }

    @Override
    public GoodsFileDTO toDTO(final GoodsFile model, final boolean forListView) {
        final GoodsFileDTO dto = new GoodsFileDTO();

        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setName(model.getName());
        dto.setSpecification(model.getSpecification());
        dto.setDescription(model.getDescription());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setManagementOrgCode(model.getManagementOrgCode());
        dto.setManagementOrgName(model.getManagementOrgName());

        if (model.getSpareType() != null) {
            dto.setSpareTypeCode(model.getSpareType().getCode());
            dto.setSpareTypeName(model.getSpareType().getName());
            dto.setMeasureUnit(model.getSpareType().getUnit());
            dto.setParentAssetTypeName(model.getSpareType().getParent().getName());
        }
        if (null != model.getAssetCode()) {
            dto.setAssetCode(model.getAssetCode());
            final Asset asset = this.assetRepository.findByCode(model.getAssetCode());
            if (null != asset) {
                dto.setAssetName(asset.getName());
            }
        }
        return dto;
    }

    public GoodsFile toModel(final GoodsFileImportVO vo) {
        final GoodsFile model = new GoodsFile();
        model.setName(vo.getName());
        model.setCode(UUID.randomUUID().toString());
        model.setSpecification(vo.getSpecification());
        model.setDescription(vo.getDescription());
        model.setUpdateTime(new Date());
        model.setAssetCode(vo.getAssetCode());
        return model;
    }

    public GoodsFileImportVO toVO(final GoodsFile model) {
        final GoodsFileImportVO vo = new GoodsFileImportVO();
        vo.setName(model.getName());
        vo.setSpecification(model.getSpecification());
        vo.setSpareTypeName(model.getSpareType().getName());
        vo.setDescription(model.getDescription());
        vo.setAssetCode(model.getAssetCode());
        return vo;
    }

}
